package com.qdxwx.manage.controller.picture;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.data.service.picture.PictureService;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.picture.Picture;
import com.qdxwx.models.picture.PictureQuery;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 图片Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/picture")
public class PictureListController extends BaseController {

    @Autowired
    private PictureService pictureService;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private MemberTypeService memberTypeService;

    @Permissions("picture:list")
    @GetMapping("/list.html")
    public String list(HttpServletRequest request, ModelMap mmap) {
        int channelId = 10;//TKConvert.toInteger(request.getParameter("channelId"));
        /*
        Channel channel = new Channel();
        channel.setModuleId(ModuleType.PICTURE.ordinal());
        channel.setIsDisabled(0);
        List<Channel> channelList = channelService.getList(channel, null);
        if (channelId < 1 && channelList.size() > 0) {
            channelId = channelList.get(0).getChannelId();
        }
        mmap.put("channelList", channelList);
        */
        mmap.put("channelId", channelId);
        return "picture/picture-list";
    }

    @Permissions("picture:list")
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String search = request.getParameter("searchKey");
        if (TKConvert.toInteger(search) > 0) {
            Picture picture = pictureService.get(TKConvert.toInteger(search));
            if (picture == null) return TableDataInfo.error("找不到指定id的记录");
            List<Picture> list = new ArrayList<>();
            list.add(picture);
            return TableDataInfo.success(list, 1);
        }
        Integer status = TKConvert.toInteger(request.getParameter("status"), null);
        PictureQuery query = new PictureQuery();
        query.setChannelId(channelId);
        query.setTitle(TKString.isNotEmpty(search) ? search : null);
        query.setStatus(status);
        List<Picture> list = pictureService.getList(query, PageRequest.form(request));
        int count = pictureService.count(query);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(count);
        return rspData;
    }

}
